package com.gz.tomcat.http;


import com.gz.utils.WebUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

//1.GzRequest 作用是封装http请求的数据
//2.比如method(get)、URI(/gzCalServlet)、还有参数列表(num1=10&num2=28)
//3.GzRequest作用等价于原生的servlet的HttpServletRequest(那个参数)
public class GzRequest {

    private String method;
    private String uri;
    private HashMap<String, String> parametersMap = new HashMap<>();//数据
    private InputStream inputStream=null;

    //构造器
    //inputstream是和http请求对应的socket关联
    public GzRequest(InputStream inputStream) {
//        GET /gzCalServlet?num1=10&num2=28 HTTP/1.1
//        Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
//        Accept-Encoding: gzip, deflate, br, zstd
        this.inputStream = inputStream;
        init(inputStream);
    }

    private void init(InputStream inputStream){
        System.out.println("init()被调用");
        try {
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(inputStream, "utf-8"));
            //先获取第一行 GET /gzCalServlet?num1=10&num2=28 HTTP/1.1
            String requestLine = bufferedReader.readLine();
            String[] s = requestLine.split(" ");
            method = s[0];
            //获取数据，先看有没有数据
            //注意不能写成requestLine.indexOf("?")我一开始因为这个弄了很久没找出来原因
            int index = s[1].indexOf("?");
            if (index == -1) {
                uri = s[1];
            } else {
                uri = s[1].substring(0, index);
                //parameters->num1=10&num2=28
                String parameters=s[1].substring(index + 1);
                String[] parametersPair = parameters.split("&");
                if(null!=parametersPair && parametersPair.length>0) {
                    for(String pair:parametersPair) {
                        String[] keyValue = pair.split("=");
                        if(keyValue.length==2) {
                            //keyValue[0]->num1
                            //keyValue[1]->10
                            parametersMap.put(keyValue[0], keyValue[1]);
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String toString() {
        return "GzRequest{" +
                "method='" + method + '\'' +
                ", uri='" + uri + '\'' +
                ", parametersMap=" + parametersMap +
                '}';
    }

    public String getParameter(String name){
        if(parametersMap.containsKey(name))
            return parametersMap.get(name);
        return "";
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }
}
